<template>
  <div>
    <div class="search-term">
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
        <el-form-item label="关键词">
          <el-input placeholder="搜索条件" v-model="searchInfo.key"></el-input>
        </el-form-item>    
        <el-form-item label="名称">
          <el-input placeholder="搜索条件" v-model="searchInfo.name"></el-input>
        </el-form-item>    
        <el-form-item label="类型">
            <el-select v-model="searchInfo.type" placeholder="请选择">
              <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
              </el-option>
            </el-select>
        </el-form-item>      
        <el-form-item>
          <el-button @click="onSubmit" type="primary">查询</el-button>
            <el-button @click="rt" type="primary">重置</el-button>
        </el-form-item>
<!--        <el-form-item>-->
<!--          <el-button @click="openDialog" type="primary">新增config表</el-button>-->
<!--        </el-form-item>-->
<!--        <el-form-item>-->
<!--          <el-popover placement="top" v-model="deleteVisible" width="160">-->
<!--            <p>确定要删除吗？</p>-->
<!--              <div style="text-align: right; margin: 0">-->
<!--                <el-button @click="deleteVisible = false" size="mini" type="text">取消</el-button>-->
<!--                <el-button @click="onDelete" size="mini" type="primary">确定</el-button>-->
<!--              </div>-->
<!--            <el-button icon="el-icon-delete" size="mini" slot="reference" type="danger">批量删除</el-button>-->
<!--          </el-popover>-->
<!--        </el-form-item>-->
      </el-form>
    </div>
    <el-table
      :data="tableData"
      @selection-change="handleSelectionChange"
      border
      ref="multipleTable"
      stripe
      style="width: 100%"
      tooltip-effect="dark"
    >
<!--    <el-table-column type="selection" width="55"></el-table-column>-->
<!--    <el-table-column label="日期" width="180">-->
<!--         <template slot-scope="scope">{{scope.row.CreatedAt|formatDate}}</template>-->
<!--    </el-table-column>-->
<!--    -->
    <el-table-column label="关键词" prop="key" width="120"></el-table-column> 
    
    <el-table-column label="名称" prop="name" width="120"></el-table-column> 
    
<!--    <el-table-column label="类型" prop="type" width="120"></el-table-column> -->
      <el-table-column label="类型" width="120">
      <template slot-scope="scope">
        <div v-for="item in options" v-if="item.value == scope.row.type">
          {{item.label}}
        </div>
      </template>
      </el-table-column>
<!--    <el-table-column label="值" prop="value" width="120"></el-table-column> -->
    
      <el-table-column label="按钮组">
        <template slot-scope="scope">
          <el-button class="table-button" @click="updateConfig(scope.row)" size="small" type="primary" icon="el-icon-edit">变更</el-button>
<!--          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteRow(scope.row)">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="page"
      :page-size="pageSize"
      :page-sizes="[10, 30, 50, 100]"
      :style="{float:'right',padding:'20px'}"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>

    <el-dialog :before-close="closeDialog" :visible.sync="dialogFormVisible" title="弹窗操作">
      <el-form :model="formData" label-position="right" label-width="80px">
         <el-form-item label="关键词:">
             <div>{{formData.key}}</div>
      </el-form-item>
       
         <el-form-item label="名称:">
             <div>{{formData.name}}</div>
      </el-form-item>
         <el-form-item label="类型:">
             <template slot-scope="scope">
                 <div v-for="item in options" v-if="item.value == formData.type">
                     {{item.label}}
                 </div>
             </template>
<!--             <el-input v-model.number="formData.type" clearable placeholder="请输入"></el-input>-->
      </el-form-item>
       
         <el-form-item label="对应信息:">
            <el-input v-if="formData.type === 0" v-model="formData.value" clearable placeholder="请输入" ></el-input>
             <el-row v-if="formData.type === 1 || formData.type === 5">
                 <el-col :span="12">
                     <img  v-if="formData.type === 1" style="max-height: 150px;max-width: 150px;" :src="formData.value" class="file">
                     <el-upload
                             :action="`/api/base/upload`"
                             :headers="{ 'x-token': token }"
                             :on-error="uploadError"
                             :on-success="uploadSuccess"
                             :show-file-list="false"
                     >
                         <el-button size="small" type="primary">点击上传</el-button>
                         </el-upload>
                 </el-col>
             </el-row>
      </el-form-item>
       </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button @click="enterDialog" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
const path = process.env.VUE_APP_BASE_API
import {
    createConfig,
    deleteConfig,
    deleteConfigByIds,
    updateConfig,
    findConfig,
    getConfigList
} from "@/api/config";  //  此处请自行替换地址
import { formatTimeToStr } from "@/utils/date";
import infoList from "@/mixins/infoList";
export default {
  name: "Config",
  components: {
  },
  mixins: [infoList],
  data() {
    return {
      options: [{
        value: 0,
        label: '文字输入'
      }, {
        value: 1,
        label: '图片上传'
      }, {
        value: 2,
        label: '下拉框'
      }, {
        value: 3,
        label: '富文本文案'
      }, {
        value: 4,
        label: '文字/图片/链接'
      }, {
        value: 5,
        label: '压缩文件'
      }
      ],
      listApi: getConfigList,
      dialogFormVisible: false,
      type: "",
      deleteVisible: false,
      multipleSelection: [],formData: {
            key:"",
            name:"",
            type:0,
            value:"",
            
      }
    };
  },
  filters: {
    formatDate: function(time) {
      if (time != null && time != "") {
        var date = new Date(time);
        return formatTimeToStr(date, "yyyy-MM-dd hh:mm:ss");
      } else {
        return "";
      }
    },
    formatBoolean: function(bool) {
      if (bool != null) {
        return bool ? "是" :"否";
      } else {
        return "";
      }
    }
  },
  methods: {
      rt(){
          this.searchInfo = {}
          this.getTableData()
      },
      uploadSuccess(res) {
          this.fullscreenLoading = false
          this.formData.value  =  res.data.file.url

      },
      uploadError() {
          this.$message({
              type: 'error',
              message: '上传失败'
          })
          this.fullscreenLoading = false
      },
      //条件搜索前端看此方法
      onSubmit() {
        this.page = 1
        this.pageSize = 10        
        this.getTableData()
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      deleteRow(row){
        this.$confirm('确定要删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
           this.deleteConfig(row);
        });
      },
      async onDelete() {
        const ids = []
        if(this.multipleSelection.length == 0){
          this.$message({
            type: 'warning',
            message: '请选择要删除的数据'
          })
          return
        }
        this.multipleSelection &&
          this.multipleSelection.map(item => {
            ids.push(item.ID)
          })
        const res = await deleteConfigByIds({ ids })
        if (res.code == 0) {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          if (this.tableData.length == ids.length && this.page > 1) {
              this.page--;
          }
          this.deleteVisible = false
          this.getTableData()
        }
      },
    async updateConfig(row) {

      if (row.type === 3){
          this.$router.push({
              name: 'type_edit',
              query: { 'id': row.ID }
          })
      }else {
          const res = await findConfig({ID: row.ID});
          this.type = "update";
          if (res.code == 0) {
              this.formData = res.data.reconfig;
              this.dialogFormVisible = true;
          }
      }
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.formData = {
          key:"",
          name:"",
          type:0,
          value:"",
          
      };
    },
    async deleteConfig(row) {
      const res = await deleteConfig({ ID: row.ID });
      if (res.code == 0) {
        this.$message({
          type: "success",
          message: "删除成功"
        });
        if (this.tableData.length == 1 && this.page > 1 ) {
            this.page--;
        }
        this.getTableData();
      }
    },
    async enterDialog() {
      let res;
      switch (this.type) {
        case "creat e":
          res = await createConfig(this.formData);
          break;
        case "update":
          res = await updateConfig(this.formData);
          break;
        default:
          res = await createConfig(this.formData);
          break;
      }
      if (res.code == 0) {
        this.$message({
          type:"success",
          message:"创建/更改成功"
        })
        this.closeDialog();
        this.getTableData();
      }
    },
    openDialog() {
      this.type = "create";
      this.dialogFormVisible = true;
    }
  },
  async created() {
    await this.getTableData();
  
}
};
</script>

<style>
</style>
